package st.business.tepth.business.user.busnumber.workholder.maintain;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.view.View;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import me.yokeyword.fragmentation.SupportFragment;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.bean.BusInfoWrapperBean;
import st.business.tepth.business.event.ReloadMaintainWorkDataEvent;
import st.business.tepth.business.user.busnumber.workholder.maintain.finish.MaintainFinishDelegate;
import st.business.tepth.business.user.busnumber.workholder.maintain.pass.MaintainPassDelegate;
import st.business.tepth.business.user.busnumber.workholder.maintain.work.WorkDelegate;
import st.business.tepth.business.user.busnumber.workholder.repair.finish.RepairCompleteDelegate;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;

/**
 * @author zhangyu
 * @date 2019/4/11
 * 保养作业中 包含(作业、过检和竣检)
 */
public class MaintainWorkHolderDelegate extends MvpFragment<MaintainWorkHolderPresenter>
        implements MaintainWorkHolderContract.View {
    public static MaintainWorkHolderDelegate newInstance(int id) {
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        MaintainWorkHolderDelegate delegate = new MaintainWorkHolderDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.layout_container)
    ConstraintLayout mContainer;
    private int mRepairId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mRepairId = bundle.getInt("id");
        }
    }

    @Override
    public MaintainWorkHolderPresenter getPresenter() {
        return new MaintainWorkHolderPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_repair_holder;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onDataChangeEvent(ReloadMaintainWorkDataEvent event) {
        if (event.mRepairId == mRepairId) {
            loadData();
        }
    }

    private void loadData() {
        //  addTopView(R.layout.view_loading, mContainer);
        mPresenter.getWorkInfo(UserManager.getInstance().getToken(), mRepairId);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        loadData();
    }


    @Override
    public void showWorkInfo(BusInfoWrapperBean workInfoBean) {
        if (workInfoBean == null) {
            removeTopView(mContainer);
            addTopView(R.layout.view_bus_empty, mContainer);
        } else {
            SupportFragment topChildFragment = (SupportFragment) getTopChildFragment();
            if (workInfoBean.getWork() != null) {
                //作业中
                if (topChildFragment != null) {
                    topChildFragment.startWithPop(WorkDelegate.newInstance(
                            workInfoBean.getWork(), workInfoBean.getId(),
                            workInfoBean.getType()));
                } else {
                    loadRootFragment(R.id.layout_content, WorkDelegate.newInstance(
                            workInfoBean.getWork(), workInfoBean.getId(), workInfoBean.getType()));
                }
            } else if (workInfoBean.getPass() != null) {
                //过检
                if (topChildFragment != null) {
                    topChildFragment.startWithPop(MaintainPassDelegate.
                            newInstance(workInfoBean.getId(),
                                    workInfoBean.getType(),
                                    workInfoBean.getPass().getOperationState()));
                } else {
                    loadRootFragment(R.id.layout_content, MaintainPassDelegate.
                            newInstance(workInfoBean.getId(),
                                    workInfoBean.getType(),
                                    workInfoBean.getPass().getOperationState()));
                }
            } else if (workInfoBean.getComplete() != null) {
                //竣检
                if (findChildFragment(RepairCompleteDelegate.class) == null) {
                    loadRootFragment(R.id.layout_content,
                            MaintainFinishDelegate.newInstance(
                                    mRepairId,
                                    workInfoBean.getType(),
                                    workInfoBean.getComplete().getOperationState()));
                }
            }
        }
    }
}
